package gwtadmin.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class FormSearch extends VerticalPanel implements ClickHandler,AsyncCallback<String> {

	private Button btnSearch = new Button("Search", this);
	private Button btnReset = new Button("Reset", this);
	
	private TextBox txtPortType = new TextBox();
	private TextBox txtNamespace = new TextBox();
	
	private RadioButton rbPortType = new RadioButton("rbSearch", "Port Type");
	private RadioButton rbService = new RadioButton("rbSearch", "Service");

	private TableOfServices tos;
	private IWsdlManager wsdlMan;

	public FormSearch(IWsdlManager wsdlManager) {

		wsdlMan = wsdlManager;
		
		tos = new TableOfServices(wsdlMan);
		tos.getElement().setAttribute("style", "margin-top: 10px");
		tos.setVisible(false);

		HorizontalPanel hp = new HorizontalPanel();
		hp.add(btnSearch);
		hp.add(btnReset);

		rbPortType.setValue(true);

		HorizontalPanel hpRadio = new HorizontalPanel();
		hpRadio.add(rbPortType);
		hpRadio.add(rbService);
		
		Grid grid = new Grid(4, 2);
		grid.setWidget(0, 1, hpRadio);
		grid.setWidget(1, 0, new Label("Port Type"));
		grid.setWidget(1, 1, txtPortType);
		grid.setWidget(2, 0, new Label("Namespace"));
		grid.setWidget(2, 1, txtNamespace);
		grid.setWidget(3, 1, hp);
		grid.addStyleName("decorFrame");
		grid.getElement().setAttribute("style", "padding: 20px");

		add(grid);
		add(tos);
	}

	public void showResult(String[] svcs) {
		tos.setServices(svcs);
		tos.setVisible(true);
	}

	public void clearResult() {
		tos.removeServices();
		tos.setVisible(false);
	}
	
	public void showWait(boolean show) {
		tos.showWait(show);
	}

	@Override
	public void onClick(ClickEvent event) {
		Button btn = (Button) event.getSource();

		if (btn == btnReset) {
			clearResult();
			txtPortType.setText("");
			txtNamespace.setText("");
		} else if (btn == btnSearch) {

			String strPortType = txtPortType.getText();
			String strNamespace = txtNamespace.getText();

			if (strPortType.equals("")) {
				Window.alert("You forget enter Port Type");
			} else if (strNamespace.equals("")) {
				Window.alert("Namespace field is empty. I don't like it!");
			} else {
				wsdlMan.searchWsdl(rbPortType.getValue(), strPortType, strNamespace, this);
			}
		}
	}

	@Override
	public void onSuccess(String messageXml) {
		if (!messageXml.equals("")) {
			String[] wsdlList = wsdlMan.parseWsdlList(messageXml);
			showResult(wsdlList);
		}
	}
	
	@Override
	public void onFailure(Throwable caught) {
		Window.alert("Error failure");
	}
}
